/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package methods;

import core.Constants;

/**
 *
 * @author Alexey Chirkov
 */
public class Yavnaya implements Method {

    private static Yavnaya obj = null;
    private int K = 4;
    private int I = 4;
    private double ht = 45;
    private double hr = 1;
    private double[][] value;

    public Yavnaya() {
        solved();
    }

    public static Method getInstance() {
        if (obj == null) {
            obj = new Yavnaya();
        }
        return obj;
    }

    private void solved() {
        value = new double[K + 1][I + 1];
        for (int i = 0; i <= I; i++) {
            value[0][i] = Constants.getU0();
        }
        for (int k = 1; k <= K; k++) {
            value[k][0] = value[k-1][0];
            double temp=(4*Constants.getK()*ht*(value[k][1]-value[k][0]))/(Constants.getC()*hr*hr);
            value[k][0]+=temp;
            temp=(2*Constants.getK()*Constants.getAlpha()*ht*value[k][0])/(Constants.getC()*Constants.getL());
            value[k][0]=value[k][0]-temp;
            temp=(ht*2*Constants.getK()*Constants.getAlpha()*Constants.getU0())/(Constants.getC()*Constants.getL());
            value[k][0]+=temp;
            temp=(Constants.getBetta()*ht*Constants.getIntensity(getRi(0)))/Constants.getC();
            value[k][0]+=temp;
            for (int i = 1; i <= I - 1; i++) {
                value[k][i] = value[k-1][i];
                double sum=(Constants.getK()*(value[k-1][i+1]-2*value[k-1][i]+value[k-1][i-1]))/(hr*hr);
                temp=(Constants.getK()*(value[k-1][i]-value[k-1][i-1]))/(hr);
                sum+=temp;
                temp=-(2*Constants.getK()*Constants.getAlpha()*value[k-1][i])/(Constants.getL());
                sum+=temp;
                temp=((2*Constants.getK()*Constants.getAlpha()*Constants.getU0())/Constants.getL())+Constants.getBetta()*Constants.getIntensity(getRi(i));
                sum+=temp;
                value[k][i]+=(ht*sum)/Constants.getC();
            }
            value[k][I] = value[k][I-1];
        }
    }

    @Override
    public double getValueAt(int k, int i) {
        return value[k][i];
    }

    @Override
    public void setK(int k) {
        this.K = k;
        this.ht = Constants.getTimeEnd() / k;
        solved();
    }

    @Override
    public int getK() {
        return K;
    }

    @Override
    public void setI(int i) {
        this.I = i;
        this.hr = Constants.getRadius() / i;
        solved();
    }

    @Override
    public int getI() {
        return I;
    }

    @Override
    public double getRi(int i) {
        return i * hr;
    }

    @Override
    public double getTk(int k) {
        return k * ht;
    }
}
